/**
 * Created by lingo on 2017/9/28.
 */
'use strict';

import Base from './base.js';
var _ = require('lodash');
export default class extends Base {

    async registerphoneAction(){
        let account = this.post("account")
        let result = await this.model('user').where({account:account}).find()
        console.log(result)
        if (think.isEmpty(result)){
            this.success(result);
        }else {
            this.fail('USER_EXIST_ERROR')
        }
    }


/*注册*/
    async registerAction(){
        let account = this.post('account')
        let password = this.post('password')
        let nickname = this.post('name')
        if (think.isEmpty(nickname)) {
            nickname = account;
        }
        //注册的时候默认的用户名为 用户账户
        let result = await this.model('user').thenAdd({account: account,password: password,username: nickname},{account: account})
        console.log(result)
        if ('exist' == result.type){
            this.fail('USER_EXIST_ERROR')
        }
        this.success(result,'注册成功')
    }
/*登录*/
    async loginAction(){
        let account = this.get('account')
        let password = this.get('password')
        console.log(account)
        let result = await this.model('user').where({account:account,password:password}).find()
        let respResult = _.omit(result,'password','createTime','updateTime')
        if (think.isEmpty(result)){
           this.fail('LOGIN_NOPERSON_ERROR')//没有这个用户
         }
         this.success(respResult)
    }

    async updateAction(){
        //查出当前用户
        let model = this.model('user')
       // let result1 = await model.where({account: 'lingo'}).update({email:'616272096@qq.com'})
        //let result2 =  model.where('1=1').update({email:'geekapplee@qq.com'})
        let result3 = model.where({id: 5}).increment('loginState', 1)
        console.log(result3)
        this.success(result3)
    }


    /*登出*/
    async logoutAction(){
        this.fail(10001,"登出失败")
    }

    async listAction(){
        let model = this.model('user')
       // let result = await model.where({account: 'lingoddd'}).find()

        //page(a,b)  a - currentPage,b - numsPerPage ; countSelect(c) c == true 为修正到第一页，c == false 为修正到最后一页
        let result = await model.page(10,10).countSelect(true)
        if (think.isEmpty(result)){
            this.fail('SELECT_DATA_ERROR')
        }
        this.success(result)
    }

    async userAction(){
        let model = this.model('user')
        let result = await model.limit(2).select()
        if (think.isEmpty(result)){
            this.fail('SELECT_DATA_ERROR')
        }
        this.success(result)
    }

    async loginstateAction(){
        let state = this.get('loginstate')
        let res = await this.model('user').where({loginState:state}).count()
        console.log(res)
        if (think.isEmpty(res)){
            this.fail('SELECT_DATA_ERROR')
        }
        this.success(res)
    }
}